import { InspectFileItemModel } from '../../core/api/models/inspect-report-model';
// components/popup/popup.js

const imgList: InspectFileItemModel[] = [];
Component({
    options: {
        styleIsolation: 'isolated'
    },
    /**
     * 组件的属性列表
     */
    properties: {
        max: {
            type: Number,
            value: 1
        },
        fileList: {
            type: Array,
            value: []
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        imgList,
        canUpload: true
    },

    /**
     * 组件的方法列表
     */
    methods: {
        chooseImage(e: any) {
            const imgList = this.data.imgList;
            wx.chooseImage({
                count: this.data.max - imgList.length,
                sizeType: ['compressed'],
                sourceType: ['album', 'camera'],
                success: (res) => {
                    const promiseList: any[] = [];
                    res.tempFilePaths.forEach((path) => {
                        promiseList.push(new Promise((resolve, reject)  => {
                            wx.saveFile({
                                tempFilePath: path,
                                success: (locPath) => {
                                    resolve({
                                        locPath: locPath.savedFilePath,
                                        fullPath: '',
                                        fileId: '',
                                        fileName: '',
                                        signUrl: ''
                                    });
                                },
                                fail: (err) => {
                                    reject(err);
                                }
                            });
                        }))
                    })

                    Promise.all(promiseList).then(list => {
                        this.addLocalFile(list);
                    }).catch(err => {
                        console.log(err);
                    })


                }
            });
        },

        onRemove: function (e: any) {
            const imgList = [...this.data.imgList];
            const index = e.target.dataset.index;
            imgList.splice(index, 1);
            this.setData({
                imgList: imgList
            });
        },

        onPreviewImg: function (e: any) {
            const index = e.target.dataset.index;
            const imgList = this.data.imgList;

            wx.previewImage({
                urls: imgList.map(i => this.getItemPath(i)),
                current: this.getItemPath(imgList[index])
            });
        },

        getItemPath: function (item: InspectFileItemModel) {
            return item.locPath || item.signUrl;
        },

        addLocalFile: function (list) {
            const {imgList} = this.data
            this.setData({
                imgList: [...imgList,...list]
            });
            this.triggerEvent('changeValue', [...imgList,...list]);
        },
        checkSimple: function (v1: InspectFileItemModel[], v2: InspectFileItemModel[]) {
            let simple = false;
            if (v1.length === v2.length) {
                simple = true;
                if (v1.length > 0) {
                    let i2: InspectFileItemModel;
                    v1.forEach((i1, index) => {
                        i2 = v2[index];
                        if (i1.locPath != i2.locPath || i1.fullPath != i2.fullPath) {
                            simple = false;
                        }
                    });
                }
            }
            return simple;
        }
    },
    lifetimes: {
        ready: function () {
        }
    },
    observers: {
        "imgList": function (list) {
            this.setData({
                canUpload: list.length < this.data.max
            });
        },
        "max": function (max) {
            this.setData({
                canUpload: this.data.imgList.length < max
            })
        },
        "fileList": function (fileList) {
            const res: InspectFileItemModel[] = [];
            const { imgList } = this.data;
            if (this.checkSimple(imgList, fileList)) {
                return;
            }
            fileList.map((file: InspectFileItemModel) => {
                res.push({
                    locPath: file.locPath,
                    fullPath: file.fullPath,
                    fileId: file.fileId || '',
                    fileName: file.fileName || '',
                    signUrl: file.signUrl || ''
                });
            });
            this.setData({
                imgList: res
            });
        }
    }
})
